package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.SupplierMapper;
import com.gxa.zhonglong.pojo.Supplier;
import com.gxa.zhonglong.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author NITRO
 */

@Service("supplierService")
@Transactional(rollbackFor = {Throwable.class})
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;


    @Override
    public List<Supplier> select(Integer page, Integer size,String supplier_name) {
        System.out.println("name:"+supplier_name);
        return supplierMapper.selectSupplierList((page-1)*size,size,supplier_name);
    }

    @Override
    public Boolean add(Supplier supplier) throws BusinessException {
        Supplier supplier1 = supplierMapper.selectById(Long.valueOf(supplier.getCode()));

       if (supplier1 == null){
           supplierMapper.addSupplier(supplier);
           return true;
       }
        throw new BusinessException(ExceptionCode.ADD_DEFEATED);
    }

    @Override
    public void delete(List<Long> ids) throws BusinessException {
        if (supplierMapper.deleteSupplier(ids)<1) {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }
    }

    @Override
    public Boolean update(Supplier supplier) throws BusinessException {
        Boolean aBoolean = supplierMapper.updateSupplier(supplier);
        if (aBoolean) {
            return true;
        }
        throw new BusinessException(ExceptionCode.UPDATE_FALSE);
    }

    @Override
    public List<Supplier> selectAll() {
        return supplierMapper.selectAll();
    }
}
